<?php
/**
 * 常用接口
 */
namespace app\smartbc\controller\api\v1;
use app\smartbc\controller\api\Base;
use app\smartbc\model\SmartbcAdwords;
use app\smartbc\model\SmartbcCoupon;
use app\smartbc\model\SmartbcCouponUser;
use app\smartbc\model\SmartbcConfig;
use think\helper\Time;

class Index extends Base{

    /**
     * 广告图片
     * @return void
     */
    public function adwords(){
        $param['ids'] = $this->request->param('ids');
        $rel = $this->apiSign($param);
        if($rel['code'] != 200){
            return enjson($rel['code'],'签名验证失败');
        }
        $group = explode('/',$param['ids']);
        $adword = [];
        foreach ($group as $value) {
            $adword[$value] = [];
        }
        $rel = SmartbcAdwords::where(['member_miniapp_id' => $this->miniapp_id,'group' => $group])->field('title,picture,link,open_type,group')->order('sort desc,id desc')->select();
        foreach ($rel as $rs) {
            $adword[$rs['group']][] = $rs;
        }
        return enjson(200,$adword);
    }

    /**
     * 查询优惠券
     * mode(new|hot|max)
     * @return void
     */
    public function coupon(){
        $param['mode'] = $this->request->param('mode/s','new');
        $param['page'] = $this->request->param('page/d',1);
        $rel = $this->apiSign($param);
        if ($rel['code'] != 200) {
            return enjson($rel['code'],'签名验证失败');
        }
        $validate = $this->validate($param,'Coupon.api');
        if(true !== $validate){
            return enjson(403,$validate);
        }
        if($param['mode'] == 'hot'){
            $condition = [['c.member_miniapp_id','=',$this->miniapp_id],['c.is_lock','=',0],['c.is_end','=',0]];
        }else{
            $condition = [['member_miniapp_id','=',$this->miniapp_id],['is_lock','=',0],['is_end','=',0]];
        }
        //查询语句
        $couponSql = SmartbcCoupon::with(['store' => function($query) {
            $query->field('id,name,img');
        }]);
        switch ($param['mode']) {
            case 'hot':
                $sql = $couponSql->alias('c')->where($condition)->join('SmartbcCouponUser u', 'c.id = u.coupon_id')->where(['u.member_miniapp_id' => $this->miniapp_id])
                    ->field('c.*,count(coupon_id) as hot')->group('u.coupon_id')->order('hot desc,c.is_top desc,c.sort desc,c.create_time desc');
                break;
            case 'max':
                $sql = $couponSql->where($condition)->field('*,IF(types > 0,howmuch - howmuch * discount/10,price) as money')->order('money desc,is_top desc,sort desc,create_time desc');
                break;
            default:
                $sql = $couponSql->where($condition)->order('is_top desc,sort desc,create_time desc');
                break;
        }
        $coupon = $sql->page($param['page'],10)->select();
        if ($coupon->isEmpty()) {
            return enjson(204,'没有优惠券');
        }
        //判断已领取
        if ($this->user) {
            $coupon_ids     = array_column($coupon->toArray(),'id');
            $user_coupon    = SmartbcCouponUser::where(['coupon_id' => $coupon_ids,'uid' => $this->user->id,'is_end'=> 0])->field('uid,store_id,coupon_id')->select()->toArray();
            $user_coupon_id = array_unique(array_column($user_coupon,'coupon_id'));
            //给已领取的加状态
            foreach ($coupon as $key => $value) {
                $coupon[$key]->is_get_coupon =  in_array($value['id'],$user_coupon_id) ? 1 : 0;
            }
        }
        return enjson(200,$coupon);
    }

    /**
     * 显示单个优惠券信息
     * @return void
     */
    public function couponInfo(){
        $param['id']        = $this->request->param('id');  //优惠券id
        $rel = $this->apiSign($param);
        if ($rel['code'] != 200) {
            return enjson($rel['code'],'签名验证失败');
        }
        $info = SmartbcCoupon::with(['couponuser' => function($query) {
            $query->field('coupon_id,is_end,create_time');
        },'store' => function($query) {
            $query->field('id,name,address,longitude,latitude,img,telphone');
        }])->where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
        if (empty($info)) {
            return enjson(404,'未找到优惠券');
        }
        $info->is_get_coupon = (empty($info->couponuser) || $info->couponuser->is_end == 1) ? 0 : 1;
        $info->store->img    =  $info->store->img."?x-oss-process=style/w100";
        if(!empty($this->qqgps)){
            $info->store->distance = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$info->store->longitude,$info->store->latitude);
        }
        return enjson(200,$info);
    }
}